/**
 * Kohive.app.SettingsController
 * @extends ExtMVC.Controller
 * Controls the Settings application
 */
Kohive.app.SettingsController = Ext.extend(Kohive.controllers.KohiveController,
		{
			name					: 'settings',

			constructor				: function() {
				Kohive.app.SettingsController.superclass.constructor.call(this,
						{
							viewsPackage	: Kohive.views.settings
						}
				);

				this.initListeners();

				// TODO: This is a relic of old MVC, remove it once on ExtMVC 0.6 or above
				this.registerAction('index', this.index);
			},

			initListeners			: function() {
				var controller = Kohive.OS.getController('idcards');

				controller.on({
							scope				: this,
							'verify-success'	: this.onVerifySuccess,
							'verify-failed'		: this.onVerifyFailure
						});
			},

			/**
			 * @action index 
			 * Renders the index view
			 */
			index					: function(pane) {
				this.indexWindow = this.renderView('index', {
					closeAction	: 'hide',
					autoShow	: true,
					listeners	: {
						'changecard'	: {
							fn	: function(panel, oldId, newId) {
								// update window title
								try {
									panel
											.setTitle(Ext.ux.Translator
													.translate('app_settings')
													+ " : "
													+ panel.mainPanel.layout.activeItem.title);
								} catch (e) {
								};
							}
						},
						'render'		: {
							scope	: this,
							fn		: this.attachChildViewEvents
						}
					}
				}
				);

				this.indexWindow.show();

				// check if a pane has been defined, and if so, try and activate it
				if (pane) {
					// check if the user needs permission
					if ((pane == "settings_view_hive_applications" || pane == "settings_view_hive_appearance")
							&& !Kohive.OS.hasPermission(true))
						return;

					// add the hiveid onto the end
					var pane = pane + Kohive.OS.getCurrentHiveId();

					// update the menu's active item
					this.indexWindow.menuPanel.setActiveItem(pane);

					// set the active item
					this.indexWindow.mainPanel.layout.setActiveItem(pane);

					// update window title
					try {
						this.indexWindow
								.setTitle("Settings : "
										+ this.indexWindow.mainPanel.layout.activeItem.title);
					} catch (e) {
					};

					// fire off the activate event
					var item = this.indexWindow.mainPanel.layout.container
							.getComponent(pane);
					if (item) {
						item.fireEvent('activate', item);
					};
				};

				this.fireEvent('index');
			},

			/**
			 * Adds the various listeners to child panels of the settings window
			 * Intended to be attached to the Window's render event
			 * @param {Ext.Window} indexWindow The main settings window
			 */
			attachChildViewEvents	: function(indexWindow) {
				this.indexWindow = this.indexWindow || indexWindow;

				this.attachChildEvent('settings_view_hive_id_card'
								+ Kohive.OS.getCurrentHiveId(), {
							'refresh'	: {
								fn	: function() {
									this.indexWindow.showRefreshing();
								}
							},
							'destroy'	: {
								fn	: function() {
									this.indexWindow.showDestroying();
								}
							},
							'verify'	: {
								fn	: function() {
									this.indexWindow.showSubmitting();
								}
							},
							'refreshed'	: {
								fn	: function() {
									this.indexWindow.hideLoading();
								}
							}
						});
			},

			/**
			 * Attaches events for a single sub view (private)
			 * @param {Ext.Window} indexWindow The main settings window
			 */
			attachChildEvent		: function(viewId, listeners) {
				// add a default scope of *this* for each listener
				for (l in listeners) {
					Ext.applyIf(listeners[l], {
								scope	: this
							});
				};

				// Add each of the listeners to the specified view
				try {
					for (l in listeners) {
						this.indexWindow.mainPanel.findById(viewId).on(l,
								listeners[l].fn, this
						);
					};
				} catch (e) {
				};
			},

			onVerifySuccess			: function() {
				if (this.indexWindow)
					this.indexWindow.showSuccess();
			},

			onVerifyFailure			: function() {
				if (this.indexWindow)
					this.indexWindow.showFailure();
			}
		}
);

Kohive.OS.registerController('settings', Kohive.app.SettingsController);